package cn.aws360.boot.data.api.utils;

import cn.aws360.boot.data.api.pojo.response.PageResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author junnan
 */
@Slf4j
@UtilityClass
public class PojoUtils {

    public static <T> List<T> copyToList(List<?> rawList, Class<T> clazz) {
        List<T> newList = new ArrayList<>();
        if (CollUtil.isNotEmpty(rawList)) {
            for (Object o : rawList) {
                newList.add(BeanUtil.copyProperties(o, clazz));
            }
        }
        return newList;
    }

    public static <T> PageResult<T> copyToPage(PageResult<?> rawPage, Class<T> clazz) {
        PageResult<T> newPage = new PageResult<>();
        BeanUtil.copyProperties(rawPage, newPage);
        newPage.setRows(copyToList(rawPage.getRows(), clazz));
        return newPage;
    }

}
